use std::sync::{Arc, RwLock};

use clap::{Args, Parser, Subcommand};
use lazy_static::lazy_static;

lazy_static! {
    pub static ref APP_CONFIG: Arc<RwLock<AppConfig>> = {
        let n = AppConfig {
            bs_db: "f.db3".to_string(),
        };
        Arc::new(RwLock::new(n))
    };
}

#[derive(Parser, Clone)]
#[command(name = "PagerBot")]
#[command(author = "PushAMP LLC. <ceo@pushamp.com>")]
#[command(version = "0.1.0")]
#[command(about = "Does messages on your pager", long_about = None)]
pub struct Cli {
    #[arg(long, env)]
    psb_host: String,
    #[arg(long, env)]
    psb_username: Option<String>,
    #[arg(long, env)]
    psb_password: String,
    #[arg(long, env, default_value_t = String::from("pager_messages_v1"))]
    topic: String,
    #[arg(long, env, default_value_t = String::from("bs_user_file.json"))]
    bs_user_file: String,
    #[command(subcommand)]
    pub command: Commands,
    #[arg(long, env, default_value_t = String::from("bs_db.db3"))]
    pub bs_db: String,
}

#[derive(Subcommand, Clone)]
pub enum Commands {
    RunBot,
    CreateUser(AddArgs),
}

#[derive(Args, Clone)]
pub struct AddArgs {
    #[arg(long)]
    pub name: String,
}

#[derive(Builder, Debug)]
#[builder(name = "AppConfigConstructor")]
pub struct AppConfig {
    bs_db: String,
}

impl From<Cli> for AppConfig {
    fn from(c: Cli) -> Self {
        AppConfigConstructor::default()
            .bs_db(c.bs_db)
            .build()
            .unwrap()
    }
}

impl AppConfig {
    pub(crate) fn get_bs_db(&self) -> String {
        self.bs_db.to_string()
    }
}
